{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "sonic-replication",
   "metadata": {},
   "source": [
    "<center><h1>第五次作业</h1></center>\n",
    "<center>3018233061 樊一飞</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "danish-angel",
   "metadata": {},
   "source": [
    "## 1.\n",
    "\n",
    "从原始函数$y=\\frac1{1+x^2},x\\in[-5,5]$,随机取二十个点$\\{x_i, y_i\\}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "encouraging-spirituality",
   "metadata": {},
   "outputs": [],
   "source": [
    "y[x_]:=1/(1+x^2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "czech-terrace",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "-0.16334    0.974013\n",
       "\n",
       "-0.359302   0.885663\n",
       "\n",
       "1.19016     0.413824\n",
       "\n",
       "-4.60965    0.044946\n",
       "\n",
       "-4.54056    0.0462606\n",
       "\n",
       "-3.51582    0.0748449\n",
       "\n",
       "3.26689     0.0856707\n",
       "\n",
       "0.441272    0.837016\n",
       "\n",
       "-4.27044    0.0519842\n",
       "\n",
       "0.575239    0.751371\n",
       "\n",
       "0.673631    0.687862\n",
       "\n",
       "1.76749     0.242483\n",
       "\n",
       "-4.06947    0.0569456\n",
       "\n",
       "3.31341     0.0834815\n",
       "\n",
       "0.757277    0.635539\n",
       "\n",
       "4.71381     0.0430663\n",
       "\n",
       "4.51384     0.0467842\n",
       "\n",
       "0.475928    0.815323\n",
       "\n",
       "2.63881     0.125576\n",
       "\n",
       "0.446091    0.83403"
      ]
     },
     "execution_count": 15,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {#,y[#]}&/@RandomReal[{-5,5},20];\n",
    "data//MatrixForm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "spread-shuttle",
   "metadata": {},
   "source": [
    "### (1)\n",
    "在[-5, 5]的区间，求二十个点的插值多项式 P10(x)和P20(x)，x最高阶为10和20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "id": "hourly-spank",
   "metadata": {},
   "outputs": [],
   "source": [
    "PolyFit[data_,n_]:=Fit[data,Table[x^i,{i,0,n}],x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "received-warehouse",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t\t.grid-container {\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t\t\tdisplay: inline-grid;\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t\t\tgrid-template-columns: auto;\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t\t}\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t</style>\n",
       "\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t<div><div class=\"grid-container\"><div class=\"grid-item\"><img alt=\"Output\" src=\"\"></div><div class=\"grid-item\"><img alt=\"Output\" src=\"\"></div></div></div>"
      ],
      "text/plain": [
       "                                  2              3             4              5\n",
       "0.985022 + 0.010552 x - 0.771711 x  - 0.0712661 x  + 0.338987 x  + 0.0784783 x  - \n",
       " \n",
       "               6              7                8               9                10\n",
       ">   0.0585534 x  - 0.0224022 x  + 0.000954759 x  + 0.00124671 x  + 0.000131047 x\n",
       "                                     2               3             4              5\n",
       "0.999998 + 0.000274323 x - 0.998546 x  - 0.00961843 x  + 0.977016 x  + 0.0866559 x  - \n",
       " \n",
       "              6             7             8             9              10\n",
       ">   0.845974 x  - 0.273493 x  + 0.458072 x  + 0.298045 x  - 0.0784225 x   - \n",
       " \n",
       "              11              12              13               14                15\n",
       ">   0.119881 x   - 0.0213001 x   + 0.0150582 x   + 0.00739317 x   + 0.000566443 x   - \n",
       " \n",
       "                 16                 17             -6  18             -7  19\n",
       ">   0.000350515 x   - 0.0000930896 x   - 5.27329 10   x   + 7.31394 10   x   + \n",
       " \n",
       "              -8  20\n",
       ">   7.95668 10   x"
      ]
     },
     "execution_count": 169,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P10 = PolyFit[data,10]\n",
    "P20 = PolyFit[data,20]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "arranged-investigation",
   "metadata": {},
   "source": [
    "### (2)\n",
    "在同一坐标平面用不同颜色画出P10(x)和P20(x)与原始函数在[-5, 5]的曲线，并将二十个点也画出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "traditional-techno",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "-Graphics-"
      ]
     },
     "execution_count": 174,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DataPointPlot = ListPlot[data,PlotStyle->{Red}];\n",
    "FunctionPlot = Plot[y[x],{x,-5,5},PlotStyle->{Dashed,Green}];\n",
    "FitPlot = Plot[{P10,P20},{x,-5,5}];\n",
    "Show[DataPointPlot,FitPlot,FunctionPlot]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "objective-holiday",
   "metadata": {},
   "source": [
    "### (3)\t观察两个插值曲线在该区间上对原始函数的逼近情况\n",
    "可以看出插值曲线在$x\\in(-2,2)$上逼近情况良好，P20效果要好于P10,但是在两侧表现不佳,为过拟合现象"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "phantom-spectacular",
   "metadata": {},
   "source": [
    "## 2.\t对题目1中重新采集20个数据，试着求分段区间的多项式插值函数\n",
    "分为三段$(-5,-1),(-1,1),(1,5)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 598,
   "id": "adjustable-luther",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t\t.grid-container {\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t\t\tdisplay: inline-grid;\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t\t\tgrid-template-columns: auto;\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t\t}\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t</style>\n",
       "\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t<div><div class=\"grid-container\"><div class=\"grid-item\"><img alt=\"Output\" src=\"\"></div><div class=\"grid-item\"><img alt=\"Output\" src=\"\"></div><div class=\"grid-item\"><img alt=\"Output\" src=\"\"></div></div></div>"
      ],
      "text/plain": [
       "                                2              3\n",
       "1.06099 + 0.768021 x + 0.20575 x  + 0.0187432 x\n",
       "                                   2              3\n",
       "0.910934 + 0.0604481 x - 0.427451 x  - 0.0653973 x\n",
       "                                 2              3\n",
       "1.02842 - 0.709824 x + 0.180294 x  - 0.0157954 x"
      ]
     },
     "execution_count": 601,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data1 = Map[Function[x,{x,y[x]}],RandomReal[{-5,-1},7]];\n",
    "data2 = Map[Function[x,{x,y[x]}],RandomReal[{-1,1},6]];\n",
    "data3 = Map[Function[x,{x,y[x]}],RandomReal[{1,5},7]];\n",
    "P1 = PolyFit[data1,3]\n",
    "P2 = PolyFit[data2,3]\n",
    "P3 = PolyFit[data3,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 617,
   "id": "tight-standing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "-Graphics-"
      ]
     },
     "execution_count": 622,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "DataPointPlot = ListPlot[{data1,data2,data3},PlotStyle->{Red}];\n",
    "FunctionPlot = Plot[y[x],{x,-5,5},PlotStyle->{Dashed,Orange}];\n",
    "P1Plot = Plot[P1,{x,-5,-1},PlotStyle->{Blue}];\n",
    "P2Plot = Plot[P2,{x,-1,1},PlotStyle->{Blue}];\n",
    "P3Plot = Plot[P3,{x,1,5},PlotStyle->{Blue}];\n",
    "Show[DataPointPlot,P1Plot,P2Plot,P3Plot,FunctionPlot,PlotRange->{0,1}]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "endless-barcelona",
   "metadata": {},
   "source": [
    "## 3.\t对二元函数 \n",
    "$z=\\sqrt{(x-2)^2+(y-3)^2}$，在x=0,1,2,3,4以及y=1,2,3,4,5的取值组合出采数据点(x,y,z)。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 550,
   "id": "norwegian-mainland",
   "metadata": {},
   "outputs": [],
   "source": [
    "ClearAll[]\n",
    "Clear[x,y]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 552,
   "id": "north-vancouver",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{{0, 1, 2 Sqrt[2]}, {0, 2, Sqrt[5]}, {0, 3, 2}, {0, 4, Sqrt[5]}, {0, 5, 2 Sqrt[2]}, \n",
       " \n",
       ">   {1, 1, Sqrt[5]}, {1, 2, Sqrt[2]}, {1, 3, 1}, {1, 4, Sqrt[2]}, {1, 5, Sqrt[5]}, \n",
       " \n",
       ">   {2, 1, 2}, {2, 2, 1}, {2, 3, 0}, {2, 4, 1}, {2, 5, 2}, {3, 1, Sqrt[5]}, \n",
       " \n",
       ">   {3, 2, Sqrt[2]}, {3, 3, 1}, {3, 4, Sqrt[2]}, {3, 5, Sqrt[5]}, {4, 1, 2 Sqrt[2]}, \n",
       " \n",
       ">   {4, 2, Sqrt[5]}, {4, 3, 2}, {4, 4, Sqrt[5]}, {4, 5, 2 Sqrt[2]}}"
      ]
     },
     "execution_count": 553,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z[x_,y_]:=Sqrt[(x-2)^2+(y-3)^2]\n",
    "data = Flatten[Table[{x,y,z[x,y]},{x,0,4},{y,1,5}],1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "strategic-classic",
   "metadata": {},
   "source": [
    "(2)\t试着在区域 对上述数据点进行二元插值，并且当x=1.3，y=2.4时的z值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 556,
   "id": "given-equipment",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "                                 2                                      2\n",
       "3.8016 + 0.128495 x - 0.0321238 x  - 1.16421 y - 1.08556 x y + 0.27139 x  y + \n",
       " \n",
       "              2               2              2  2\n",
       ">   0.194034 y  + 0.180927 x y  - 0.0452316 x  y"
      ]
     },
     "execution_count": 556,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fitZ = Fit[data,Flatten[Table[x^i y^j,{i,0,2},{j,0,2}],1],{x,y}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 557,
   "id": "literary-milton",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><pre style=\"&#102;&#111;&#110;&#116;&#45;&#102;&#97;&#109;&#105;&#108;&#121;&#58;&#32;&#34;&#67;&#111;&#117;&#114;&#105;&#101;&#114;&#32;&#78;&#101;&#119;&#34;&#44;&#67;&#111;&#117;&#114;&#105;&#101;&#114;&#44;&#109;&#111;&#110;&#111;&#115;&#112;&#97;&#99;&#101;&#59;\">&#48;&#46;&#56;&#54;&#54;&#49;&#56;&#53;</pre></div>"
      ],
      "text/plain": [
       "0.866185"
      ]
     },
     "execution_count": 557,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fitZ/.{x->1.3,y->2.4}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "vocal-neutral",
   "metadata": {},
   "source": [
    "(3)\t画出插值曲面与原始二元函数曲面 ，并进行比较。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 558,
   "id": "complete-squad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "-Graphics3D-"
      ]
     },
     "execution_count": 558,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Plot3D[{z[x,y],fitZ},{x,0,5},{y,1,5}]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "automotive-samoa",
   "metadata": {},
   "source": [
    "## 4.\t从原始函数 \n",
    "$$\n",
    "y=\\frac1{1+e^{-x}}\n",
    "$$\n",
    "在x轴每隔0.5采一个数据点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 393,
   "id": "graphic-brighton",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "-5.          0.00669285\n",
       "\n",
       "-4.5         0.0109869\n",
       "\n",
       "-4.          0.0179862\n",
       "\n",
       "-3.5         0.0293122\n",
       "\n",
       "-3.          0.0474259\n",
       "\n",
       "-2.5         0.0758582\n",
       "\n",
       "-2.          0.119203\n",
       "\n",
       "-1.5         0.182426\n",
       "\n",
       "-1.          0.268941\n",
       "\n",
       "-0.5         0.377541\n",
       "\n",
       "0.           0.5\n",
       "\n",
       "0.5          0.622459\n",
       "\n",
       "1.           0.731059\n",
       "\n",
       "1.5          0.817574\n",
       "\n",
       "2.           0.880797\n",
       "\n",
       "2.5          0.924142\n",
       "\n",
       "3.           0.952574\n",
       "\n",
       "3.5          0.970688\n",
       "\n",
       "4.           0.982014\n",
       "\n",
       "4.5          0.989013\n",
       "\n",
       "5.           0.993307"
      ]
     },
     "execution_count": 396,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ClearAll[]\n",
    "y[x_]:= 1/(1+Exp[-x])\n",
    "data = Table[{x,y[x]},{x,-5,5,.5}];\n",
    "data//MatrixForm"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "major-establishment",
   "metadata": {},
   "source": [
    "(1)\t试着对上述数据点进行多项式插值，画出插值曲面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 400,
   "id": "alert-alignment",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "                             -17  2              3             -18  4                5\n",
       "0.5 + 0.227436 x + 8.57925 10    x  - 0.0098082 x  - 4.46965 10    x  + 0.000188583 x"
      ]
     },
     "execution_count": 400,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fitted = PoltFit[data,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 401,
   "id": "modified-import",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "-Graphics-"
      ]
     },
     "execution_count": 401,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Plot[{y[x],Fitted},{x,-5,5}]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "handmade-burke",
   "metadata": {},
   "source": [
    "(2)\t画出上述数据点中各邻近点直接直线相连的线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 412,
   "id": "amazing-valuation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "-Graphics-"
      ]
     },
     "execution_count": 412,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ListLinePlot[data]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "banner-beatles",
   "metadata": {},
   "source": [
    "## 5.\t设x的取值为1到50的整数，根据x构造50个点(x,y)其中y为第x个质数\n",
    "(1)\t输出上述50个点；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "executive-acrylic",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><pre style=\"&#102;&#111;&#110;&#116;&#45;&#102;&#97;&#109;&#105;&#108;&#121;&#58;&#32;&#34;&#67;&#111;&#117;&#114;&#105;&#101;&#114;&#32;&#78;&#101;&#119;&#34;&#44;&#67;&#111;&#117;&#114;&#105;&#101;&#114;&#44;&#109;&#111;&#110;&#111;&#115;&#112;&#97;&#99;&#101;&#59;\">&#123;&#50;&#44;&#32;&#51;&#44;&#32;&#53;&#44;&#32;&#55;&#44;&#32;&#49;&#49;&#44;&#32;&#49;&#51;&#44;&#32;&#49;&#55;&#44;&#32;&#49;&#57;&#44;&#32;&#50;&#51;&#44;&#32;&#50;&#57;&#44;&#32;&#51;&#49;&#44;&#32;&#51;&#55;&#44;&#32;&#52;&#49;&#44;&#32;&#52;&#51;&#44;&#32;&#52;&#55;&#44;&#32;&#53;&#51;&#44;&#32;&#53;&#57;&#44;&#32;&#54;&#49;&#44;&#32;&#54;&#55;&#44;&#32;&#55;&#49;&#44;&#32;&#55;&#51;&#44;&#32;&#55;&#57;&#44;&#32;&#56;&#51;&#44;&#32;&#10;&#32;&#10;&#62;&#32;&#32;&#32;&#56;&#57;&#44;&#32;&#57;&#55;&#44;&#32;&#49;&#48;&#49;&#44;&#32;&#49;&#48;&#51;&#44;&#32;&#49;&#48;&#55;&#44;&#32;&#49;&#48;&#57;&#44;&#32;&#49;&#49;&#51;&#44;&#32;&#49;&#50;&#55;&#44;&#32;&#49;&#51;&#49;&#44;&#32;&#49;&#51;&#55;&#44;&#32;&#49;&#51;&#57;&#44;&#32;&#49;&#52;&#57;&#44;&#32;&#49;&#53;&#49;&#44;&#32;&#49;&#53;&#55;&#44;&#32;&#49;&#54;&#51;&#44;&#32;&#49;&#54;&#55;&#44;&#32;&#49;&#55;&#51;&#44;&#32;&#10;&#32;&#10;&#62;&#32;&#32;&#32;&#49;&#55;&#57;&#44;&#32;&#49;&#56;&#49;&#44;&#32;&#49;&#57;&#49;&#44;&#32;&#49;&#57;&#51;&#44;&#32;&#49;&#57;&#55;&#44;&#32;&#49;&#57;&#57;&#44;&#32;&#50;&#49;&#49;&#44;&#32;&#50;&#50;&#51;&#44;&#32;&#50;&#50;&#55;&#44;&#32;&#50;&#50;&#57;&#125;</pre></div>"
      ],
      "text/plain": [
       "{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, \n",
       " \n",
       ">   89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, \n",
       " \n",
       ">   179, 181, 191, 193, 197, 199, 211, 223, 227, 229}"
      ]
     },
     "execution_count": 16,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "primes = Table[Prime[x],{x,50}]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "convenient-toolbox",
   "metadata": {},
   "source": [
    "(2)\t用最高三阶多项式进行拟合，并作出点和曲线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 417,
   "id": "rental-chemistry",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t\t.grid-container {\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t\t\tdisplay: inline-grid;\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t\t\tgrid-template-columns: auto;\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t\t}\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t</style>\n",
       "\n",
       "\t\t\t\t\t\t\t\t\t\t\t\t<div><div class=\"grid-container\"><div class=\"grid-item\"><img alt=\"Output\" src=\"\"></div><div class=\"grid-item\"><img alt=\"Output\" src=\"\"></div></div></div>"
      ],
      "text/plain": [
       "                                 2                3\n",
       "-3.3174 + 2.59398 x + 0.0616932 x  - 0.000413771 x\n",
       "-Graphics-"
      ]
     },
     "execution_count": 417,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fitted = PolyFit[primes,3]\n",
    "Show[ListPlot[primes,PlotStyle->{Red}],Plot[Fitted,{x,1,50}]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "digital-salvation",
   "metadata": {},
   "source": [
    "(3)\t用$a x \\log(b+c x)$进行拟合，求出a,b,c，并作出点和曲线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "material-access",
   "metadata": {},
   "outputs": [],
   "source": [
    "model[x_]:=a x Log[b+c x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cathedral-enzyme",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{a -> 1.28036, b -> 1.92947, c -> 0.677361}"
      ]
     },
     "execution_count": 25,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LogFitArgs = FindFit[primes,model[x],{a,b,c},x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "split-atmosphere",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "1.28036 x Log[1.92947 + 0.677361 x]"
      ]
     },
     "execution_count": 26,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LogFit = model[x] /. LogFitArgs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "victorian-anxiety",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "-Graphics-"
      ]
     },
     "execution_count": 27,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Show[\n",
    "    ListPlot[primes,PlotStyle->{Red}],\n",
    "    Plot[LogFit,{x,1,50}]\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cheap-landscape",
   "metadata": {},
   "source": [
    "(4)\t试着求出更好的拟合函数，并作出点和曲线(*).\n",
    "$$\n",
    "y=ax \\log(b+cx+dx^2)\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 461,
   "id": "overhead-boring",
   "metadata": {},
   "outputs": [
    {
     "ename": "FindFit::cvmit",
     "evalue": "Failed to converge to the requested accuracy or precision within 100 iterations.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31mFindFit::cvmit: Failed to converge to the requested accuracy or precision within 100 iterations.\u001b[0m"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{a -> 0.91653, b -> 0.603416, c -> 1.80857, d -> 0.0232353}"
      ]
     },
     "execution_count": 461,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LogFitArgs = FindFit[primes,a x Log[b+c x+d x^2],{a,b,c,d},x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 462,
   "id": "apparent-diving",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "                                                2\n",
       "0.91653 x Log[0.603416 + 1.80857 x + 0.0232353 x ]"
      ]
     },
     "execution_count": 462,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LogFit = a x Log[b+c x+d x^2]/.LogFitArgs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 463,
   "id": "false-pregnancy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "-Graphics-"
      ]
     },
     "execution_count": 463,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Show[\n",
    "    ListPlot[primes,PlotStyle->{Red}],\n",
    "    Plot[LogFit,{x,1,50}]\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "arctic-cleaner",
   "metadata": {},
   "source": [
    "## 6.\t求解下面的线性规划问题：\n",
    "(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 465,
   "id": "downtown-trailer",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       " 18\n",
       "{--, 5}\n",
       " 7"
      ]
     },
     "execution_count": 465,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LinearProgramming[\n",
    "    {-10,-62},\n",
    "    {{1,1},{7,9},{1,0},{0,1}},\n",
    "    {{1,1},{63,-1},{6,-1},{5,-1}}\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "welsh-standing",
   "metadata": {},
   "source": [
    "(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 466,
   "id": "insured-administration",
   "metadata": {},
   "outputs": [
    {
     "ename": "LinearProgramming::lpsub",
     "evalue": "This problem is unbounded.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31mLinearProgramming::lpsub: This problem is unbounded.\u001b[0m"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{Indeterminate, Indeterminate}"
      ]
     },
     "execution_count": 466,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LinearProgramming[\n",
    "    {-1.5,-2.5},\n",
    "    {{1,3},{1,1}},\n",
    "    {1,2}\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "prescription-shadow",
   "metadata": {},
   "source": [
    "## 7.\t求解下面的线性规划问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 468,
   "id": "stuck-luxembourg",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "       3\n",
       "{0, 0, -, 0}\n",
       "       4"
      ]
     },
     "execution_count": 468,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "LinearProgramming[\n",
    "    {12,8,16,12},\n",
    "    {{2,1,4,0},{2,2,4,0}},\n",
    "    {2,3}\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deluxe-retail",
   "metadata": {},
   "source": [
    "## 8.\t求函数\n",
    "$$\n",
    "z=e^{2x}(x+y^2+2y)\n",
    "$$\n",
    "在区间$[-1, 1]\\times[-2, 1]$内的极值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 625,
   "id": "incorporate-wright",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "-Graphics3D-"
      ]
     },
     "execution_count": 626,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z[x_,y_]:=Exp[2x](x+y^2+2y)\n",
    "Plot3D[z[x,y],{x,-1,1},{y,-2,1}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 475,
   "id": "identical-survival",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "  1\n",
       "-(-) E\n",
       "  2"
      ]
     },
     "execution_count": 475,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MinValue[{z[x,y],-1<=x<=1,-2<=y<=1},{x,y}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 476,
   "id": "addressed-ghana",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "   2\n",
       "4 E"
      ]
     },
     "execution_count": 476,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MaxValue[{z[x,y],-1<=x<=1,-2<=y<=1},{x,y}]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "terminal-luxury",
   "metadata": {},
   "source": [
    "## 9.\t求函数 \n",
    "$$\n",
    "f(x,y,z)=x^4+\\sin y-\\cos z\n",
    "$$\n",
    "在点（0, 5, 4）附件的极小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 477,
   "id": "first-owner",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{-2., {x -> 0., y -> 4.71239, z -> 6.28319}}"
      ]
     },
     "execution_count": 477,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FindMinimum[x^4+Sin[y]-Cos[z],{x,0},{y,5},{z,4}]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "determined-entrance",
   "metadata": {},
   "source": [
    "## 10.\t求下列非线性规划\n",
    "(1)\t$\\omega=e^{-x^2} \\sin 6x$,在x0=1附件的极小点与极小值；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 479,
   "id": "crazy-celebration",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{-0.557277, {x -> 0.744838}}"
      ]
     },
     "execution_count": 479,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FindMinimum[Exp[-x^2] Sin[6x],{x,1}]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "geographic-olive",
   "metadata": {},
   "source": [
    "(2)\t$W$的极小点与极小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 484,
   "id": "hourly-collectible",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{-72., {x1 -> 1., x2 -> 2., x3 -> 3.}}"
      ]
     },
     "execution_count": 484,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FindMinimum[x1^3+x2^3+x3^3-3(x1+4 x2+9 x3),{x1,x2,x3}]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "south-cylinder",
   "metadata": {},
   "source": [
    "## 11.\t试写出一个函数f(x)，用FindMinimum[f,x]求出来的最小值不是真正的最小值，但是加入初始条件是可以求出真正的最小值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "touched-technique",
   "metadata": {},
   "outputs": [],
   "source": [
    "f[x_]:=x Sin[x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "empirical-thesis",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "-Graphics-"
      ]
     },
     "execution_count": 5,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Plot[f[x],{x,-18,18}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ceramic-contribution",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{1.81971, {x -> 2.02876}}"
      ]
     },
     "execution_count": 3,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FindMaximum[{f[x],-18<=x<=18},x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "interesting-burning",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{14.1724, {x -> -14.2074}}"
      ]
     },
     "execution_count": 6,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FindMaximum[{f[x],-18<=x<=18},{x,-15}]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "moved-interim",
   "metadata": {},
   "source": [
    "## 12.\t试着通过一些例子来归纳MaxValue，FindMaximum，Maximizate的不同之处"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 537,
   "id": "dutch-development",
   "metadata": {},
   "outputs": [
    {
     "ename": "MaxValue::ztest1",
     "evalue": "Unable to decide whether numeric quantity -(Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}] Sin[Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}]]) + Root[{Sin[#1] + Cos[#1] #1 & , 14.2074367251911883598}] Sin[Root[{Sin[#1] + Cos[#1] #1 & , 14.2074367251911883598}]] is equal to zero. Assuming it is.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31mMaxValue::ztest1: Unable to decide whether numeric quantity -(Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}] Sin[Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}]]) + Root[{Sin[#1] + Cos[#1] #1 & , 14.2074367251911883598}] Sin[Root[{Sin[#1] + Cos[#1] #1 & , 14.2074367251911883598}]] is equal to zero. Assuming it is.\u001b[0m"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}] \n",
       " \n",
       ">   Sin[Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}]]"
      ]
     },
     "execution_count": 537,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "MaxValue[{f[x],-18<=x<=18},x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 538,
   "id": "eight-utilization",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{1.81971, {x -> 2.02876}}"
      ]
     },
     "execution_count": 538,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FindMaximum[{f[x],-18<=x<=18},x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 539,
   "id": "false-bernard",
   "metadata": {},
   "outputs": [
    {
     "ename": "Maximize::ztest1",
     "evalue": "Unable to decide whether numeric quantity -(Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}] Sin[Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}]]) + Root[{Sin[#1] + Cos[#1] #1 & , 14.2074367251911883598}] Sin[Root[{Sin[#1] + Cos[#1] #1 & , 14.2074367251911883598}]] is equal to zero. Assuming it is.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31mMaximize::ztest1: Unable to decide whether numeric quantity -(Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}] Sin[Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}]]) + Root[{Sin[#1] + Cos[#1] #1 & , 14.2074367251911883598}] Sin[Root[{Sin[#1] + Cos[#1] #1 & , 14.2074367251911883598}]] is equal to zero. Assuming it is.\u001b[0m"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><img alt=\"Output\" src=\"\"></div>"
      ],
      "text/plain": [
       "{Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}] \n",
       " \n",
       ">    Sin[Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}]], \n",
       " \n",
       ">   {x -> Root[{Sin[#1] + Cos[#1] #1 & , -14.2074367251911883598}]}}"
      ]
     },
     "execution_count": 539,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Maximize[{f[x],-18<=x<=18},x]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 540,
   "id": "french-browser",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div><pre style=\"&#102;&#111;&#110;&#116;&#45;&#102;&#97;&#109;&#105;&#108;&#121;&#58;&#32;&#34;&#67;&#111;&#117;&#114;&#105;&#101;&#114;&#32;&#78;&#101;&#119;&#34;&#44;&#67;&#111;&#117;&#114;&#105;&#101;&#114;&#44;&#109;&#111;&#110;&#111;&#115;&#112;&#97;&#99;&#101;&#59;\">&#49;&#46;&#56;&#49;&#57;&#55;&#49;</pre></div>"
      ],
      "text/plain": [
       "1.81971"
      ]
     },
     "execution_count": 540,
     "metadata": {
      "text/html": [],
      "text/plain": []
     },
     "output_type": "execute_result"
    }
   ],
   "source": [
    "FindMaxValue[{f[x],-18<=x<=18},x]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sitting-catch",
   "metadata": {},
   "source": [
    "### 异同分析\n",
    "\n",
    "| 命令         | 功能             | 最优解性质 | 初值依赖性     | 解的类型   | 返回值            |\n",
    "| ------------ | ---------------- | ---------- | -------------- | ---------- | ----------------- |\n",
    "| MaxValue     | 求最值           | 全局       | 不依赖         | 精确解     | 最值              |\n",
    "| Maximizate   | 求最值及最值点   | 全局       | 不依赖         | 精确解     | {最值，x->最值点} |\n",
    "| FindMaxValue | 搜寻极值         | 局部       | 依赖初值的选取 | 近似数值解 | 极值              |\n",
    "| FindMaximum  | 搜寻极值及极值点 | 局部       | 依赖初值的选取 | 近似数值解 | {极值，x->极值点} |\n",
    "\n",
    "#### 最值与极值\n",
    "\n",
    "最值在紧集（一般是闭集）内一定存在，但是极值不一定存在\n",
    "\n",
    "最值不一定是极值，极值也不一定是最值\n",
    "\n",
    "#### 精确解与数值解\n",
    "\n",
    "精确解存在不一定可以表示，但数值解存在一定可算\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "pointed-symposium",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Wolfram",
   "language": "Wolfram Language",
   "name": "wolframlanguage12.2"
  },
  "language_info": {
   "codemirror_mode": "mathematica",
   "file_extension": ".m",
   "mimetype": "application/vnd.wolfram.m",
   "name": "Wolfram Language",
   "pygments_lexer": "mathematica",
   "version": "12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
